package fr.ants.controller.command;

import javax.swing.JOptionPane;

import fr.ants.controller.clock.Clock;
import fr.ants.model.Nest;
import fr.ants.model.Node;
import fr.ants.model.Resource;
import fr.ants.view.panel.Canvas;

/**
 * To move a graphic component in the map.
 * 
 * @author Guillaume Ruscassie
 * 
 */
public class MoveCommand implements Command {

	/**
	 * Canvas.
	 */
	private final Canvas canvas;

	/**
	 * Clock
	 */
	private final Clock clock;

	/**
	 * to know the new position x.
	 */
	private final int x;
	/**
	 * to know the new position y.
	 */
	private final int y;

	/**
	 * Build.
	 * 
	 * @param canvas
	 *            canvas
	 * @param clock
	 *            clock
	 * @param x
	 *            to know the new position x
	 * @param y
	 *            to know the new position y
	 */
	public MoveCommand(final Canvas canvas, final Clock clock, final int x,
			final int y) {
		this.canvas = canvas;
		this.clock = clock;
		this.x = x;
		this.y = y;
	}

	/**
	 * To move a Resource or a Nest on the map. (if the destination node is not
	 * a Nest, an obstacle or a resource)
	 * 
	 * @author Guillaume Ruscassie, Jordan ENSFELDER
	 * 
	 */
	@Override
	public void execute() {
		if (canvas.getSelectedGridX() != null
				&& canvas.getSelectedGridY() != null) {
			if (canvas.getSelectedGridX() != null
					&& canvas.getSelectedGridY() != null) {
				Node node = clock
						.getSimulator()
						.getGrid()
						.getNode(canvas.getSelectedGridX(),
								canvas.getSelectedGridY());
				if (node.getResource() != null) {

					int xx = x / Canvas.NODE_SIZE;
					int yy = y / Canvas.NODE_SIZE;
					Node newNode = clock.getSimulator().getGrid()
							.getNode(xx, yy);

					if (newNode != null && newNode.isWalkable()
							&& newNode.getNest() == null
							&& newNode.getResource() == null) {
						Resource r = node.getResource();
						node.setResource(null);
						newNode.setResource(r);
					} else if (node.getX() != newNode.getX()
							&& node.getY() != newNode.getY()) {
						JOptionPane
								.showMessageDialog(
										null,
										"Vous ne pouvez pas déplacer la ressource ici!",
										"Erreur de déplacement de la ressource",
										1);
					}
				}
				if (node.getNest() != null) {
					int xx = x / Canvas.NODE_SIZE;
					int yy = y / Canvas.NODE_SIZE;
					Node newNode = clock.getSimulator().getGrid()
							.getNode(xx, yy);

					if (newNode != null && newNode.isWalkable()
							&& newNode.getNest() == null
							&& newNode.getResource() == null) {
						Nest nest = node.getNest();
						node.setNest(null);
						newNode.setNest(nest);
					} else if (node.getX() != newNode.getX()
							&& node.getY() != newNode.getY()) {
						JOptionPane
								.showMessageDialog(
										null,
										"Vous ne pouvez pas déplacer la fourmilière ici!",
										"Erreur de déplacement de la fourmilière",
										1);
					}
				}
				canvas.setSelectedXY(null, null);
				if (!clock.isRunning()) {
					canvas.repaint();
				}
			}
		}
	}

}
